package com.bw.module_home.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bw.module_home.R
import com.bw.module_home.databinding.HomeItemSimpleInfoBinding
import com.bw.module_home.model.entity.SimpleVideoEntity

/**
 * @Author : ZhouZiYao
 * @Time : On 2023/11/23 11:23
 * @Description :
 */
class SimpleRecycleViewAdapter: RecyclerView.Adapter<SimpleRecycleViewAdapter.SimpleRecycleViewHolder>() {

    companion object{
        const val PAYLOAD_FOCUS = "focus"
        const val PAYLOAD_LIKE = "like"
    }

    private val list:MutableList<SimpleVideoEntity> = mutableListOf()

    /**
     * 局部增量刷新
     */
    fun updateItem(item: SimpleVideoEntity, position: Int,payload: String){
        list[position] = item
        notifyItemChanged(position,payload)
    }

    fun getDataByPosition(position: Int): SimpleVideoEntity?{
        if (list != null && list.size > position && position >= 0){
            return list[position]
        }
        return null
    }

    fun setList(list: MutableList<SimpleVideoEntity>){
        this.list.clear()
        this.list.addAll(list)
        notifyDataSetChanged()
    }
    fun addList(list: MutableList<SimpleVideoEntity>){
        this.list.addAll(list)
        notifyDataSetChanged()
    }
    fun clearList(){
        this.list.clear()
        notifyDataSetChanged()
    }
    fun addItem(item: SimpleVideoEntity){
        list.add(item)
        notifyDataSetChanged()
    }
    fun removeItem(item: SimpleVideoEntity){
        list.remove(item)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SimpleRecycleViewHolder {
        val binding = HomeItemSimpleInfoBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return SimpleRecycleViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(
        holder: SimpleRecycleViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        payloads.forEach {
            if(it == PAYLOAD_FOCUS){
                if(list[position].fouces){
                    holder.binding.btnItemSimpleInfoSubscibe.text = "已关注"
                }else{
                    holder.binding.btnItemSimpleInfoSubscibe.text = "关注"
                }
            }else if(it == PAYLOAD_LIKE){
                if (list[position].like){
                    holder.binding.ivItemSimpleInfoAgree.setImageDrawable(holder.itemView.context.getDrawable(R.drawable.ic_thumb_up_check))
                }else {
                    holder.binding.ivItemSimpleInfoAgree.setImageDrawable(
                        holder.itemView.context.getDrawable(
                            R.drawable.ic_agree
                        )
                    )
                }
            }
        }

        super.onBindViewHolder(holder, position, payloads)
    }

    override fun onBindViewHolder(holder: SimpleRecycleViewHolder, position: Int) {
        holder.binding.video = list[position]

        //根据状态ui
        if(list[position].like){
            holder.binding.ivItemSimpleInfoAgree.setImageDrawable(holder.itemView.context.getDrawable(R.drawable.ic_thumb_up_check))
        }else{
            holder.binding.ivItemSimpleInfoAgree.setImageDrawable(holder.itemView.context.getDrawable(R.drawable.ic_agree))
        }

        //点击播放按钮
        holder.binding.ivItemSimpleInfoPlay.setOnClickListener {
            playListener?.invoke(list[position])
        }
        //点击关注
        holder.binding.btnItemSimpleInfoSubscibe.setOnClickListener {
            focusListener?.invoke(list[position],position)
        }
        //点击点赞
        holder.binding.ivItemSimpleInfoAgree.setOnClickListener {
            likeListener?.invoke(list[position],position)
        }
    }

    class SimpleRecycleViewHolder(val binding: HomeItemSimpleInfoBinding): RecyclerView.ViewHolder(binding.root) {

    }

    var playListener: ((SimpleVideoEntity)->Unit)? = null
    //关注
    var focusListener:((SimpleVideoEntity,Int)->Unit)?=null
    //点赞
    var likeListener:((SimpleVideoEntity,Int)->Unit)?=null
}